//
//  LGDiseaseOverviewMonitorCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/16.
//

#import "LGDiseaseOverviewMonitorCell.h"
#import "LGCollectionViewLeftFlowLayout.h"

@interface LGDiseaseOverviewMonitorCell()<UICollectionViewDataSource,UICollectionViewDelegate>
/**   */
@property (nonatomic , strong)UICollectionView *collectionView;
/**   */
@property (nonatomic , strong)NSArray *titleArray;
/**   */
@property (nonatomic , strong)NSArray *unitArray;
@end

@implementation LGDiseaseOverviewMonitorCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.titleArray = @[@"症状",@"血压",@"心率",@"体重",@"血糖",@"出入量"];
        self.unitArray = @[@"",@"mmHg",@"次/分钟",@"",@"nmol/L",@"cm"];
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self.contentView);
            make.left.equalTo(self.contentView).offset(viewPix(16));
            make.right.equalTo(self.contentView).offset(-viewPix(16));
            make.height.equalTo(@(viewPix(181)));
        }];
    }
    return  self;
}
-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    [self.collectionView reloadData];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 6;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LGMonitorItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.title = self.titleArray[indexPath.item];
    cell.unit = self.unitArray[indexPath.item];
    if(indexPath.item < self.dataArray.count){
        cell.dataDic = self.dataArray[indexPath.item];
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger index = 0;
    if(indexPath.item == 1 || indexPath.item == 2){
        index = 2;
    }else if (indexPath.item == 3){
        index = 4;
    }else if (indexPath.item == 4){
        index = 3;
    }else if (indexPath.item == 5){
        index = 5;
    }
    if(self.delegate && [self.delegate respondsToSelector:@selector(checkMonitorDetail:)]){
        [self.delegate checkMonitorDetail:index];
    }
}

-(UICollectionView *)collectionView{
    if(!_collectionView){
        LGCollectionViewLeftFlowLayout *layout = [[LGCollectionViewLeftFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.itemSize = CGSizeMake((Screen_W-viewPix(56)-viewPix(16))/3.0, viewPix(80));
        layout.minimumInteritemSpacing = viewPix(8);
        layout.minimumLineSpacing = viewPix(8);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(12), Screen_W-viewPix(32), viewPix(180)) collectionViewLayout:layout];
        [_collectionView registerClass:[LGMonitorItemCell class] forCellWithReuseIdentifier:@"cell"];
        _collectionView.contentInset = UIEdgeInsetsMake(viewPix(12), viewPix(12), 0, viewPix(12));
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.scrollEnabled = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }
    return _collectionView;
}



@end


#pragma mark
#pragma mark ====> LGMonitorItemCell
@interface LGMonitorItemCell()

/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UIImageView *iconView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIImageView *tipView;
/**   */
@property (nonatomic , strong)UILabel *dateLabel;
/**   */
@property (nonatomic , strong)UILabel *contentLabel;
/**   */
@property (nonatomic , strong)UILabel *valueLabel;
/**   */
@property (nonatomic , strong)UILabel *unitLabel;
/**   */
@property (nonatomic , strong)UILabel *arrowLabel;

@end

@implementation LGMonitorItemCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

-(void)setTitle:(NSString *)title{
    _title = title;
    self.titleLabel.text = title;
    self.iconView.image = [UIImage imageNamed:[NSString stringWithFormat:@"体征监测-%@",title]];
}

-(void)setUnit:(NSString *)unit{
    _unit = unit;
    _unitLabel.text = unit;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.dateLabel.text = LGNSString(dataDic[@"date"]).length>0?LGNSString(dataDic[@"date"]):@"尚未记录";
    self.valueLabel.text = LGNSString(dataDic[@"content"]).length>0?LGNSString(dataDic[@"content"]):@"--";
    CGFloat leftMargin = viewPix(8);
    if([self.title isEqualToString:@"症状"]){
        self.valueLabel.hidden = YES;
        self.contentLabel.hidden = NO;
        self.contentLabel.text = LGNSString(dataDic[@"content"]).length>0?LGNSString(dataDic[@"content"]):@"--";
    }else if ([self.title isEqualToString:@"体重"]){
        self.contentLabel.hidden = NO;
        self.valueLabel.hidden = NO;
        self.contentLabel.text = @"BMI";
        leftMargin = viewPix(34);
    }else if ([self.title isEqualToString:@"出入量"]){
        self.contentLabel.hidden = NO;
        self.valueLabel.hidden = NO;
        self.contentLabel.text = @"腹围";
        leftMargin = viewPix(40);
    }else{
        self.contentLabel.hidden = YES;
        self.valueLabel.hidden = NO;
        self.contentLabel.text = @" ";
    }
    if([self.title isEqualToString:@"体重"]){
        NSString *status = LGNSString(dataDic[@"status"]);
        NSDictionary *titleDic = @{@"0":@"正常",@"1":@"偏胖",@"2":@"肥胖",@"3":@"偏瘦"};
        NSDictionary *colorDic = @{@"0":@"#429BFF",@"1":@"#FF9704",@"2":@"#E53C39",@"3":@"#3DC997"};
        self.arrowLabel.textColor = [UIColor colorWithString:colorDic[status]];
        self.arrowLabel.text = titleDic[status];
    }else{
        self.arrowLabel.text = @"";
    }
    [self.valueLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(leftMargin);
    }];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self.contentView addSubview:self.baseView];
    [self.baseView addSubview:self.iconView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.tipView];
    [self.baseView addSubview:self.dateLabel];
    [self.baseView addSubview:self.contentLabel];
    [self.baseView addSubview:self.valueLabel];
    [self.baseView addSubview:self.unitLabel];
    [self.baseView addSubview:self.arrowLabel];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self.contentView);
    }];
    [self.tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView).offset(-viewPix(8));
        make.centerY.equalTo(self.iconView);
        make.width.height.equalTo(@(viewPix(12)));
    }];
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(viewPix(8));
        make.right.equalTo(self.baseView).offset(-viewPix(8));
        make.top.equalTo(self.baseView).offset(viewPix(35));
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.dateLabel.mas_bottom).offset(viewPix(9));
        make.left.equalTo(self.baseView).offset(viewPix(8));
        make.right.equalTo(self.baseView).offset(-viewPix(8));
    }];
    [self.valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(viewPix(8));
        make.centerY.equalTo(self.contentLabel);
    }];
    [self.unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.valueLabel.mas_right).offset(viewPix(4));
        make.centerY.equalTo(self.valueLabel);
    }];
    [self.arrowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.centerY.equalTo(self.unitLabel);
    }];
}
-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] init];
        _baseView.backgroundColor = [UIColor colorWithString:@"#F0F7FF"];
        _baseView.cornerRidus = 10;
    }
    return _baseView;
}
-(UIImageView *)iconView{
    if(!_iconView){
        _iconView = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(8), viewPix(10), viewPix(16), viewPix(16))];
    }
    return _iconView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(26), viewPix(8), viewPix(50), viewPix(20));
    }
    return _titleLabel;
}

-(UIImageView *)tipView{
    if(!_tipView){
        _tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip12"]];
    }
    return _tipView;
}

-(UILabel *)dateLabel{
    if(!_dateLabel){
        _dateLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(12) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _dateLabel;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _contentLabel;
}

-(UILabel *)valueLabel{
    if(!_valueLabel){
        _valueLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _valueLabel;
}

-(UILabel *)unitLabel{
    if(!_unitLabel){
        _unitLabel = [UILabel lableWithText:@"" colorString:@"#999999" textFont:LGFont(12) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _unitLabel;
}

-(UILabel *)arrowLabel{
    if(!_arrowLabel){
        _arrowLabel = [UILabel lableWithText:@"" colorString:@"#EB5757" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _arrowLabel;
}

@end
